//
//  WTBarrageManager.m
//  WTLiveSDKDemo
//
//  Created by ben on 2016/11/25.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTBarrageManager.h"
#import "NSTimer+AutoStop.h"


@interface WTBarrageManager ()
@property (nonatomic,strong) NSMutableArray<WTBarrageSendModel*>*    barrages;
@property (nonatomic,strong) NSMutableArray<NSString*>*               barrage_send_to_user_ids;
@property (nonatomic,strong) NSTimer*           barrageTimer;
@property (nonatomic,strong) NSOperationQueue*  barrageOperationQueue;
@property (nonatomic,strong) dispatch_queue_t   barrageQueue;
@end


@implementation WTBarrageManager

- (instancetype)init{
    self = [super init];
    if (self) {
        
        self.barrages = [NSMutableArray arrayWithCapacity:0];
        self.barrage_send_to_user_ids = [NSMutableArray arrayWithCapacity:0];

        self.barrageOperationQueue = [[NSOperationQueue alloc] init];
        self.barrageOperationQueue.maxConcurrentOperationCount = 1;
        self.barrageQueue = dispatch_queue_create(NULL, NULL);
        
        __weak __typeof(self)weakSelf = self;
        if(!self.barrageTimer){
            self.barrageTimer = [NSTimer scheduledTimerWithAutoTimeInterval:5 target:self block:^(NSTimer *timer) {
                [weakSelf timeToDealGift];
            } userInfo:nil repeats:YES];
        }
    }
    return self;
}

+ (instancetype)sharedManager{
    
    static WTBarrageManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[WTBarrageManager alloc] init];
        
    });
    return manager;
}

- (void)addBarrage:(WTBarrageSendModel *)barrage send_to_user_id:(NSString *)send_to_user_id{
    
    dispatch_barrier_sync(self.barrageQueue, ^{
        
        [self.barrages addObject:barrage];
        
        if(![self.barrage_send_to_user_ids containsObject:send_to_user_id]){
            [self.barrage_send_to_user_ids addObject:send_to_user_id];
        }
    });
}

#pragma mark
#pragma mark 处理消息
- (void)timeToDealGift{
    
    NSArray* tmpChatMessageContainer = @[];
    if(self.barrages.count <= 20){
        tmpChatMessageContainer = [self.barrages copy];
    }else {
        tmpChatMessageContainer = [[self.barrages subarrayWithRange:NSMakeRange(0, 20)] copy];
    }
    
    if(tmpChatMessageContainer.count <= 0){
        return;
    }
    __weak __typeof(self)weakSelf = self;
    for(NSString* send_to_user_id in self.barrage_send_to_user_ids){
        
        NSArray* models = [tmpChatMessageContainer filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"barrage_send_to_user_id = %@",send_to_user_id]];
        if(models && models.count > 0){

            WTBarrageOperation*  op = [WTBarrageOperation sendBarrageOperation:models anchor_id:send_to_user_id finishedBlock:^(BOOL sucess, NSNumber *leftCoins) {
                if(!sucess){
                    [weakSelf.barrages addObjectsFromArray:tmpChatMessageContainer];
                }else {
                    if(weakSelf.updateLeftCoinBlock){
                        weakSelf.updateLeftCoinBlock(leftCoins);
                    }
                    NSLog(@"余额:%@",leftCoins);
                }

            }];
            [self.barrageOperationQueue addOperation:op];
        }
    }

    
    dispatch_barrier_sync(self.barrageQueue, ^{
        [self.barrages removeObjectsInArray:tmpChatMessageContainer];
    });
}


@end

